﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using IBatisNet.DataMapper;
using EIA.Basic.DAL.IBatisMapping;
using DotNETX.Utility;

namespace EIA.Basic.DAL.IBatis
{
    public class Dao<TModel> : IDao<TModel>
    {
        protected ISqlMapper _sqlMapper = null;
        private LogInfo log = new LogInfo();

        public Dao()
        {
            _sqlMapper = SqlMapperHelper.GetSqlMapper();
        }

        #region IDao<TModel>

        public virtual object Insert(TModel model)
        {
            string statement = string.Format("Insert{0}", typeof(TModel).Name);
            try
            {
                object key = _sqlMapper.Insert(statement, model);
                return key;
            }
            catch (Exception ex)
            {
                log.WriteLog(statement + ":" + ex.Message + "---" + ex.InnerException.ToString());
                return 0;
            }
        }

        public object Insert(IDictionary props)
        {
            string statement = string.Format("Insert{0}", typeof(TModel).Name);
            try
            {
                object key = _sqlMapper.Insert(statement, props);
                return key;
            }
            catch (Exception ex)
            {
                log.WriteLog(statement + ":" + ex.Message + "---" + ex.InnerException.ToString());
                return 0;
            }
        }

        public TModel Get(object id)
        {
            string statement = string.Format("Select{0}", typeof(TModel).Name);
            try
            {
                TModel model = _sqlMapper.QueryForObject<TModel>(statement, id);
                return model;
            }
            catch (Exception ex)
            {
                log.WriteLog(statement + ":" + ex.Message + "---" + ex.InnerException.ToString());
                return default(TModel);
            }
        }

        public IList<TModel> List(IDictionary props)
        {
            try
            {
                string statement = string.Format("Select{0}_CV", typeof(TModel).Name);
                IList<TModel> models = _sqlMapper.QueryForList<TModel>(statement, props);
                //log.WriteLog("AAA");
                return models;
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message + "---" + ex.InnerException.ToString());
                return null;
            }
        }

        public int Count(IDictionary props)
        {
            string statement = string.Format("Select{0}_PC", typeof(TModel).Name);
            int count = _sqlMapper.QueryForObject<int>(statement, props);
            return count;
        }

        public IList<TModel> List(IDictionary props, int pagesize, int page)
        {
            try
            {
                int offset = pagesize * (page - 1);
                int count = Count(props);
                int rows = offset + pagesize - count > 0 ? count - offset : pagesize;
                string statement = string.Format("Select{0}_PG", typeof(TModel).Name);
                if (props == null) props = new Dictionary<string, object>();
                props["_offset"] = offset;
                props["_rows"] = rows;
                IList<TModel> models = _sqlMapper.QueryForList<TModel>(statement, props);
                props.Remove("_offset");
                props.Remove("_rows");
                return models;
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message + "---" + ex.InnerException.ToString());
                return null;
            }
        }

        public virtual bool Update(TModel model)
        {
            string statement = string.Format("Update{0}", typeof(TModel).Name);
            try
            {
                int effect = _sqlMapper.Update(statement, model);
                return effect > 0;
            }
            catch (Exception ex)
            {
                log.WriteLog(statement + ":" + ex.Message + "---" + ex.InnerException.ToString());
                return false;
            }
        }

        public virtual bool Update(IDictionary props)
        {
            string statement = string.Format("Update{0}", typeof(TModel).Name);
            try
            {
                int effect = _sqlMapper.Update(statement, props);
                return effect > 0;
            }
            catch (Exception ex)
            {
                log.WriteLog(statement + ":" + ex.Message + "---" + ex.InnerException.ToString());
                return false;
            }
        }

        public int Delete(object id)
        {
            string statement = string.Format("Delete{0}", typeof(TModel).Name);
            try
            {
                int effect = _sqlMapper.Delete(statement, id);
                return effect;
            }
            catch (Exception ex)
            {
                log.WriteLog(statement + ":" + ex.Message + "---" + ex.InnerException.ToString());
                return 0;
            }
        }

        public int Delete(TModel model)
        {
            string statement = string.Format("Delete{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, model);
            return effect;
        }

        public int Delete(IDictionary props)
        {
            string statement = string.Format("Delete{0}_CV", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, props);
            return effect;
        }

        public virtual int Clear()
        {
            string statement = string.Format("Clear{0}", typeof(TModel).Name);
            int effect = _sqlMapper.Delete(statement, null);
            return effect;
        }

        private IDASession _daSession = null;
        public virtual IDASession DASession
        {
            get
            {
                if (null == _daSession) { _daSession = new DASession(_sqlMapper); }
                return _daSession;
            }
        }

        #endregion
    }
}
